import {promisify_wx as wx} from './promisify_wx.js'
let domain = 'https://znjj.test.hbtubi.com';
let header = {
  'content-type': 'application/json'
}
let request = (api_route, data = {}, method = 'GET') => {
  let token = wx.getStorageSync('token');
  if (token) {
    header.token = token
  }
  return wx.request({
    url: domain + api_route,
    data,
    method,
    header
  }).then(res => {
    const data = res.data
    return new Promise((resolve, reject) => {
      if (data.code == 1) {
        resolve(data)
      } else {
        reject(data)
      }
    })
  })
}
exports.request = request;
exports.domain = domain;
/* ### 定义接口 ### */
// 测试
exports.test = () => request('/api/demo/test1', {}, 'GET')
// 商家登陆
exports.businessLogin = (account, password) => request('/api/user/login', {account, type: 'business', password}, 'POST')
// 师傅登陆
exports.workerLogin = (account, password) => request('/api/user/login', {account, type: 'worker', password}, 'POST')
// 用户微信登陆
exports.userLogin = (code) => request('/api/user/wechat_mini', {code}, 'GET')
// 修改会员个人信息
exports.userProfile = (nickname,mobile,address,avatar,password) => request('/api/user/profile', {nickname,mobile,address,avatar,password}, 'POST')
//退出登录
exports.userLogout = () => request('/api/user/logout', {}, 'POST')
//获取个人信息
exports.getUserInfo = () => request('/api/user/getUserInfo', {}, 'POST')
// 刷新Token
let refreshToken = () => request('/api/token/refresh', {}, 'GET').then(res => {
  if (res.code == 1) {
    let data = res.data
    wx.setStorageSync('token', data.token)
  }
})
exports.refreshToken = refreshToken;
// 检测Token是否过期
exports.checkToken = () => request('/api/token/check', {}, 'GET').then(res => {
  if (res.data.expires_in < 1800) {
    refreshToken()
  }
  return Promise.resolve(res);
}).catch(err => {
  wx.setStorageSync('token', null)
  wx.setStorageSync('userinfo', null)
  wx.setStorageSync('loginType', null)
  return Promise.reject(err)
})
// 首页数据
exports.indexData = (city_id = 0) => request('/api/customer.index/index?city_id=' + city_id, {}, 'GET')
// 获取地理位置 city_id
exports.getAddress = (latitude,longitude) => request('/api/customer.index/getAddress?latitude=' + latitude + '&longitude=' + longitude, {}, 'GET')
// 商家列表
exports.businessList = (city_id = 0, latitude = null, longitude = null, page = 1) => request('/api/customer.index/business_list?city_id=' + city_id + '&latitude=' + latitude + '&longitude=' + longitude + '&page=' + page, {}, 'GET')
// 支付预定定单
exports.pay = (order_sn) => request('/api/customer.pay/pay', {order_sn}, 'POST')
// 我的页面数据
exports.myIndex = (city_id = 0) => request('/api/customer.my.index/index?city_id='+city_id, {}, 'GET')
// 客户-我的设备列表
exports.customerProductList = (page = 1) => request('/api/customer.my.user_product/list?page='+page, {}, 'GET')
// 客户-添加设备
exports.customerAddProduct = (brand_id, model_id) => request('/api/customer.my.user_product/add', {brand_id, model_id}, 'POST')
// 客户-删除设备
exports.customerDeleteProduct = (id) => request('/api/customer.my.user_product/delete', {id}, 'POST')
// 客户-产品预定定单列表
exports.customerProductOrderList = (status = 'all', page = 1) => request('/api/customer.order.product/list?status='+status+'&page='+page, {}, 'GET')
// 客户-产品定单详情
exports.customerProductOrderDetail = (order_sn) => request('/api/customer.order.product/detail?order_sn='+order_sn, {}, 'GET')
// 客户-创建产品定单
exports.customerCreateProductOrder = (business_user_id,product_id,num) => request('/api/customer.order.product/create', {business_user_id,product_id,num}, 'POST')
// 客户-安装订单列表
exports.customerInstallOrderList = (status = 'all', page = 1) => request('/api/customer.order.install/list?status='+status+'&page='+page, {}, 'GET')
//客户-获取本地商家列表
exports.getBusinessList = (cityId) => request('/api/customer.order.install/business_list?cityId='+cityId, {}, 'GET')
// 客户-安装订单详情
exports.customerInstallOrderDetail = (order_sn) => request('/api/customer.order.install/detail?order_sn='+order_sn, {}, 'GET')
// 客户-安装订单点击完成操作
exports.customerCompleteInstallOrder = (order_sn) => request('/api/customer.order.install/complete', {order_sn}, 'POST')
// 客户-安装订单评论
exports.customerCommentInstallOrder = (order_sn, comment) => request('/api/customer.order.install/comment', {order_sn, comment}, 'POST')
// 客户-保养订单列表
exports.customerMaintenanceOrderList = (status = 'all', page = 1) => request('/api/customer.order.maintenance/list?status='+status+'&page='+page, {}, 'GET')
// 客户-保养订单详情
exports.customerMaintenanceOrderDetail = (order_sn) => request('/api/customer.order.maintenance/detail?order_sn='+order_sn, {}, 'GET')
// 客户-创建保养订单
exports.customerCreateMaintenanceOrder = (service_id, door_type, contacts, contact_number, province_id, city_id, area_id, address, latitude, longitude, pre_book_time, problem_description, images) => request('/api/customer.order.maintenance/createOrder', {service_id, door_type, contacts, contact_number, province_id, city_id, area_id, address, latitude, longitude, pre_book_time, problem_description, images}, 'POST')
// 客户-修改保养订单
exports.customerModifyMaintenanceOrder = (order_sn, service_id, door_type, contacts, contact_number, province_id, city_id, area_id, address, latitude, longitude, pre_book_time, problem_description, images) => request('/api/customer.order.maintenance/modifyOrder', {order_sn, service_id, door_type, contacts, contact_number, province_id, city_id, area_id, address, latitude, longitude, pre_book_time, problem_description, images}, 'POST')
// 客户-删除保养订单
exports.customerDeleteMaintenanceOrder = (order_sn) => request('/api/customer.order.maintenance/deleteOrder', {order_sn}, 'POST')
// 客户-保养订单点击完成操作
exports.customerCompleteMaintenanceOrder = (order_sn) => request('/api/customer.order.maintenance/complete', {order_sn}, 'POST')
// 客户-保养订单评论
exports.customerCommentMaintenanceOrder = (order_sn, comment) => request('/api/customer.order.maintenance/comment', {order_sn, comment}, 'POST')

// 客户-维修订单列表
exports.customerRepairOrderList = (status = 'all', page = 1) => request('/api/customer.order.repair/list?status='+status+'&page='+page, {}, 'GET')
// 客户-维修订单详情
exports.customerRepairOrderDetail = (order_sn) => request('/api/customer.order.repair/detail?order_sn='+order_sn, {}, 'GET')
// 客户-创建维修订单
exports.customerCreateRepairOrder = (category_id, door_type, contacts, contact_number, province_id, city_id, area_id, address, latitude, longitude, pre_book_time, problem_description, images) => request('/api/customer.order.repair/createOrder', {category_id, door_type, contacts, contact_number, province_id, city_id, area_id, address, latitude, longitude, pre_book_time, problem_description, images}, 'POST')
// 客户-修改维修订单
exports.customerModifyRepairOrder = (order_sn, category_id, door_type, contacts, contact_number, province_id, city_id, area_id, address, latitude, longitude, pre_book_time, problem_description, images) => request('/api/customer.order.repair/modifyOrder', {order_sn, category_id, door_type, contacts, contact_number, province_id, city_id, area_id, address, latitude, longitude, pre_book_time, problem_description, images}, 'POST')
// 客户-删除维修订单
exports.customerDeleteRepairOrder = (order_sn) => request('/api/customer.order.repair/deleteOrder', {order_sn}, 'POST')
// 客户-维修订单点击完成操作
exports.customerCompleteRepairOrder = (order_sn) => request('/api/customer.order.repair/complete', {order_sn}, 'POST')
// 客户-维修订单评论
exports.customerCommentRepairOrder = (order_sn, comment) => request('/api/customer.order.repair/comment', {order_sn, comment}, 'POST')
// 自有产品分类
exports.productCategoryList= () => request('/api/customer.product/category_list', {}, 'GET')
// 自有商品列表
exports.productList= (category_id, page = 1) => request('/api/customer.product/product_list?category_id='+category_id+'&page='+page, {}, 'GET')
// 自有商品详情
exports.productDetail = (id) => request('/api/customer.product/product_detail?id='+id, {}, 'GET')
// 维修类目列表
exports.repairCategoryList= () => request('/api/customer.repair/category_list', {}, 'GET')
// 安装品牌列表
exports.installBrandList= () => request('/api/customer.install/brand_list', {}, 'GET')
// 安装锁体型号
exports.installModelList= (brand_id=0) => request('/api/customer.install/model_list?brand_id='+brand_id, {}, 'GET')
// 保养服务列表
exports.maintenanceServiceList= () => request('/api/customer.maintenance/service_list', {}, 'GET')
// 文章详情
exports.articleDetail = (id) => request('/api/customer.article/detail?id='+id, {}, 'GET')
// 商家-产品预定定单列表
exports.businessProductOrderList= (status = 'all', page = 1) => request('/api/business.order.product/list?status='+status+'&page='+page, {}, 'GET')
// 商家-产品定单详情
exports.businessProductOrderDetail = (order_sn) => request('/api/business.order.product/detail?order_sn='+order_sn, {}, 'GET')
// 商家-核销订单
exports.businessProductOrderWriteOff = (order_sn, code) => request('/api/business.order.product/writeOffOrder', {order_sn, code}, 'POST')
// 商家-安装订单列表
exports.businessInstallOrderList= (status = 'all', page = 1) => request('/api/business.order.install/list?status='+status+'&page='+page, {}, 'GET')
// 商家-安装订单详情
exports.businessInstallOrderDetail = (order_sn) => request('/api/business.order.install/detail?order_sn='+order_sn, {}, 'GET')
//通过手机号获取个人id
exports.getUserId = (mobile) => request('/api/user/getUserId', {mobile}, 'POST')
// 商家-创建安装订单
exports.businessCreateInstallOrder = (customer_user_id, brand_id, model_id, door_type, num, contacts, contact_number, province_id, city_id, area_id, address, latitude, longitude, install_time, remarks,lock_model) => request('/api/business.order.install/createOrder', {customer_user_id, brand_id, model_id, door_type, num, contacts, contact_number, province_id, city_id, area_id, address, latitude, longitude, install_time, remarks,lock_model}, 'POST')
// 商家-修改安装订单
exports.businessModifyRepairOrder = (order_sn, customer_user_id, brand_id, model_id, door_type, num, contacts, contact_number, province_id, city_id, area_id, address, latitude, longitude, install_time, remarks) => request('/api/business.order.install/modifyOrder', {order_sn, customer_user_id, brand_id, model_id, door_type, num, contacts, contact_number, province_id, city_id, area_id, address, latitude, longitude, install_time, remarks}, 'POST')
// 商家-删除安装订单
exports.businessDeleteRepairOrder = (order_sn) => request('/api/business.order.install/deleteOrder', {order_sn}, 'POST')
// 商家-提现列表
exports.businessWithdrawList = (status = 'all', page = 1) => request('/api/business/withdraw/list', {status, page}, 'GET')
// 商家-提现申请
exports.businessAddWithdraw = (money, account, account_type, memo = '') => request('/api/business/withdraw/add', {money, account, account_type, memo}, 'POST')
//商家提现
exports.isShowWithdraw = () => request('/api/business/withdraw/show', {}, 'POST')
// 师傅-安装订单列表
exports.workerInstallOrderList= (status = 'all', page = 1) => request('/api/worker.order.install/list?status='+status+'&page='+page, {}, 'GET')
// 师傅-订单位置列表
exports.workerOrderMarkers= (order_type = 'install') => request('/api/worker.order.install/markers?order_type='+order_type, {}, 'GET')
// 师傅-安装订单详情
exports.workerInstallOrderDetail = (order_sn) => request('/api/worker.order.install/detail?order_sn='+order_sn, {}, 'GET')
// 师傅-安装订单上传图片
exports.workerInstallOrderUploadImages = (order_sn,images) => request('/api/worker.order.install/add_install_images', {order_sn,images}, 'POST')
// 师傅-安装订单删除图片
exports.workerInstallOrderDelImages = (order_sn,image_key) => request('/api/worker.order.install/del_install_images', {order_sn,image_key}, 'POST')
// 师傅-保养订单列表
exports.workerMaintenanceOrderList= (status = 'all', page = 1) => request('/api/worker.order.maintenance/list?status='+status+'&page='+page, {}, 'GET')
// 师傅-保养订单详情
exports.workerMaintenanceOrderDetail = (order_sn) => request('/api/worker.order.maintenance/detail?order_sn='+order_sn, {}, 'GET')
// 师傅-保养订单上传图片
exports.workerMaintenanceOrderUploadImages = (order_sn,images) => request('/api/worker.order.maintenance/add_maintenance_images', {order_sn,images}, 'POST')
// 师傅-保养订单删除图片
exports.workerMaintenanceOrderDelImages = (order_sn,image_key) => request('/api/worker.order.maintenance/del_maintenance_images', {order_sn,image_key}, 'POST')
// 师傅-维修订单列表
exports.workerRepairOrderList= (status = 'all', page = 1) => request('/api/worker.order.repair/list?status='+status+'&page='+page, {}, 'GET')
// 师傅-维修订单详情
exports.workerRepairOrderDetail = (order_sn) => request('/api/worker.order.repair/detail?order_sn='+order_sn, {}, 'GET')
// 师傅-保养订单上传图片
exports.workerRepairOrderUploadImages = (order_sn,images) => request('/api/worker.order.repair/add_repair_images', {order_sn,images}, 'POST')
// 师傅-保养订单删除图片
exports.workerRepairOrderDelImages = (order_sn,image_key) => request('/api/worker.order.repair/del_repair_images', {order_sn,image_key}, 'POST')
// 根据邮编获取地区ID
exports.getAreaByCode = (code) => request('/api/common/area', {code}, 'POST')
// 根据名称获取地区ID
exports.getAreaByName = (city_name,area_name) => request('/api/common/area_name', {city_name,area_name}, 'POST')

let requestFile = (api_route, data = {}, method = 'POST') => {
  let token = wx.getStorageSync('token');
  if (token) {
    header.token = token
  }
  return wx.request({
    url: domain + api_route,
    data,
    method,
    header,
    responseType: 'arraybuffer'
  })
}
// 获取核销二维码
exports.writeOffQrCode = (order_sn) => requestFile('/api/common/writeOffQrCode', {order_sn}, 'POST')
// 上传七牛云
exports.upload = (filePath, formData = {}) => {
  let token = wx.getStorageSync('token');
  return wx.uploadFile({
    filePath,
    name: 'file',
    url: domain + '/api/common/upload_qiniu',
    formData: formData,
    header: {
      'content-type': 'multipart/form-data',
      token
    },
    timeout: 0,
    // success: (res) => {
    //   console.log(res)
    //   const data = res.data
    // },
    // fail: (err) => {console.log(err)},
    // complete: (res) => {console.log(res)},
  })
  .then(res => {
    const data = JSON.parse(res.data)
    return new Promise((resolve, reject) => {
      if (data.code == 1) {
        resolve(data)
      } else {
        reject(data)
      }
    })
  })
  // .onProgressUpdate((res) => {
  //   console.log('上传进度', res.progress)
  //   console.log('已经上传的数据长度', res.totalBytesSent)
  //   console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
  // })
}